/**
 * 
 */
package gui;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.TreeCellEditor;

/**
 * @author Administrator
 * 
 */
public class CheckNodeTreeCellEditor extends AbstractCellEditor implements TreeCellEditor
{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -485383808110910587L;

	/** The renderer. */
	private CheckNodeTreeCellRenderer renderer = new CheckNodeTreeCellRenderer();

	/** The change event. */
	ChangeEvent changeEvent = null;

	/** The tree. */
	private JTree tree;

	public CheckNodeTreeCellEditor(JTree tree)
	{
		this.tree = tree;
	}

	/*
	 * (@Inheritance doc)
	 * @see javax.swing.CellEditor#getCellEditorValue()
	 */
	public Object getCellEditorValue()
	{
		JCheckBox checkbox = this.renderer.getNodeRenderer();
		CheckNode checkNode = new CheckNode(this.renderer.getUserObject());
		checkNode.setSelected(checkbox.isSelected());
		return checkNode;
		// return "xxx";
	}

	/*
	 * (@Inheritance doc)
	 * @see javax.swing.tree.TreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree,
	 * java.lang.Object, boolean, boolean, boolean, int)
	 */
	public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected,
	        boolean expanded, boolean leaf, int row)
	{

		Component editor = this.renderer.getTreeCellRendererComponent(tree, value, true, expanded,
		        leaf, row, true);
		// JCheckBox editor = new JCheckBox(this.renderer.getUserObject().toString());

		// editor always selected / focused
		ItemListener itemListener = new ItemListener()
		{
			public void itemStateChanged(ItemEvent itemEvent)
			{
				if (CheckNodeTreeCellEditor.this.stopCellEditing())
				{
					CheckNodeTreeCellEditor.this.fireEditingStopped();
				}
			}
		};
		if (editor instanceof JCheckBox)
		{
			((JCheckBox) editor).addItemListener(itemListener);
		}

		return editor;
		// return new JCheckBox("xxx");
	}

	public boolean isCellEditable(EventObject event)
	{
		return true;
	}

}
